package com.spa.domain.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.admin.AdminRoleAddCommand;
import com.spa.application.command.admin.AdminRoleEditCommand;
import com.spa.domain.entity.SysRole;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.mapper.SysRoleMapperStruct;
import com.spa.infrastructure.repository.condition.SysRoleCondition;
import com.spa.infrastructure.repository.po.RoleMenuRefPO;
import com.spa.infrastructure.repository.po.RoleShopRefPO;
import com.spa.infrastructure.repository.po.SysRolePO;
import com.spa.infrastructure.repository.service.RoleMenuRefRepositoryService;
import com.spa.infrastructure.repository.service.RoleShopRefRepositoryService;
import com.spa.infrastructure.repository.service.SysRoleRepositoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色  domain
 *
 * @author: lig
 * @date: 2023-12-23
 */
@Service
public class SysRoleService {

    @Resource
    private SysRoleRepositoryService sysRoleRepositoryService;

    @Resource
    private RoleMenuRefRepositoryService roleMenuRefRepositoryService;

    @Resource
    private RoleShopRefRepositoryService roleShopRefRepositoryService;

    @Transactional
    public SysRolePO addRole(AdminRoleAddCommand command) {
        SysRolePO sysRolePo = new SysRolePO();
        sysRolePo.setRoleName(command.getRoleName());
        sysRolePo.setStatus(WhetherTypeEnum.$0.getCode().toString());
        sysRolePo.setDelFlag(WhetherTypeEnum.$0.getCode().toString());
        sysRolePo.setRoleKey("");
        sysRolePo.setRoleSort(5);
        sysRoleRepositoryService.saveRole(sysRolePo);
        command.getNewPermission().forEach(mid -> {
            RoleMenuRefPO roleMenuRefPO = new RoleMenuRefPO();
            roleMenuRefPO.setMenuId(Long.valueOf(mid));
            roleMenuRefPO.setRoleId(sysRolePo.getRoleId());
            roleMenuRefRepositoryService.add(roleMenuRefPO);
        });
        command.getDataPermission().forEach(sid -> {
            RoleShopRefPO roleShopRefPO = new RoleShopRefPO();
            roleShopRefPO.setShopId(Long.valueOf(sid));
            roleShopRefPO.setRoleId(sysRolePo.getRoleId());
            roleShopRefRepositoryService.add(roleShopRefPO);
        });
        return sysRolePo;

    }

    @Transactional
    public void editRole(AdminRoleEditCommand command) {
        long roleId = command.getId();
        roleShopRefRepositoryService.deleteByRoleId(roleId);
        command.getDataPermission().forEach(sid -> {
            RoleShopRefPO roleShopRefPO = new RoleShopRefPO();
            roleShopRefPO.setShopId(Long.valueOf(sid));
            roleShopRefPO.setRoleId(roleId);
            roleShopRefRepositoryService.add(roleShopRefPO);
        });
        roleMenuRefRepositoryService.deleteByRoleId(roleId);
        command.getNewPermission().forEach(mid -> {
            RoleMenuRefPO roleMenuRefPO = new RoleMenuRefPO();
            roleMenuRefPO.setMenuId(Long.valueOf(mid));
            roleMenuRefPO.setRoleId(roleId);
            roleMenuRefRepositoryService.add(roleMenuRefPO);
        });
    }

    public Page<SysRole> pageRole(SysRoleCondition condition) {
        Page<SysRolePO> sysRolePOPage = sysRoleRepositoryService.page(condition);
        return SysRoleMapperStruct.INSTANCE.po2Entity4Page(sysRolePOPage);

    }

    public List<SysRole> listAllRole() {
        SysRoleCondition condition = new SysRoleCondition();
        List<SysRolePO> sysRolePOPage = sysRoleRepositoryService.list(condition);
        return SysRoleMapperStruct.INSTANCE.po2Entity4List(sysRolePOPage);

    }

    public SysRole infoById(Long roleId) {
        SysRole sysRole = SysRoleMapperStruct.INSTANCE.po2Entity(sysRoleRepositoryService.getById(roleId));
        List<RoleMenuRefPO> roleMenuRefPOS = roleMenuRefRepositoryService.listByRoleId(roleId);
        sysRole.setMenuIdList(roleMenuRefPOS.stream().map(RoleMenuRefPO::getMenuId).map(String::valueOf).toList());
        List<RoleShopRefPO> roleShopRefPOS = roleShopRefRepositoryService.listByRoleId(roleId);
        sysRole.setDataPermission(roleShopRefPOS.stream().map(RoleShopRefPO::getShopId).map(String::valueOf).toList());
        return sysRole;
    }

}
